package agngui.event;

import java.util.LinkedList;
import java.util.List;

/**
 * Singleton event publisher
 * 
 * @author Ben Bloom
 * @author Phil Johnson
 */
public class AGNPublisher {

	/**
	 * the instance of the publisher
	 */
	private static AGNPublisher publisher = new AGNPublisher();
	
	/**
	 * @return A handle to the publisher
	 */
	public static AGNPublisher getInstance() { return publisher; }
	
	/**
	 * all listeners registered in the system
	 */
	private static List<AGNListener> listeners;
	
	/**
	 * constructs the publisher
	 */
	private AGNPublisher() {
		listeners = new LinkedList<AGNListener>();
	}
	
	/**
	 * registers a listener to receive events in the system
	 * 
	 * @param listener The listener to receive events
	 */
	public static void addAGNListener(AGNListener listener) {
    	listeners.add(listener);
    }
    
	/**
	 * dispatches the given event to all registered listeners
	 * 
	 * @param event The event to dispatch
	 */
    public static void dispatchEvent(AGNEvent event) {
    	for(AGNListener listener : listeners) {
    		listener.processEvent(event);
    	}
    }
}
